package com.proposition.paper.service.impl;

import com.proposition.commons.constant.ErrorCode;
import com.proposition.commons.constant.ExamCategory;
import com.proposition.commons.enums.PaperPattern;
import com.proposition.commons.model.StrategyTwo;
import com.proposition.paper.service.AbstractPaperAddStrategy;
import com.proposition.paper.service.PaperAddStrategy;
import com.proposition.paper.service.PaperStrategyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 客观化组卷策略
 *
 * @author 杨凯
 * @create 2018-02-03 8:54
 **/
@Service("keguanhua")
public class KeGuanHuaServiceImpl extends AbstractPaperAddStrategy implements PaperAddStrategy {

	@Autowired
	private PaperStrategyService paperStrategyService;

	@Override
	public int firstQuestion(String exam) {
		String[] exams = examSplit(exam,",");
		//判断试题是否存在重复
		boolean flag = checkRepeat(exams);
		if(!flag){
			return 3004;
		}
		//判断穴位顺序是否正确（穴位顺序部位为从上往下）
		int status = checAcupointAdult(exams);
		if(status != ErrorCode.SUCCESS){
			//穴位顺序存在问题
			return status;
		}
		return ErrorCode.SUCCESS;
	}

	@Override
	public int secondQuestion(String exam) {
		String[] exams = examSplit(exam,",");
		String[] examCategory = getExamCategory(exam);
		//判断试题是否存在重复
		boolean flag = checkRepeat(exams);
		if(!flag){
			//单项手法存在重复
			return 3007;
		}
		//判断是否为成人手法
		boolean flag1 = isIdentical(examCategory);
		if(!flag1){
			return 3008;
		}
		//判断是否同一大类
		int status = checkTechnique(exams);
		if(status != ErrorCode.SUCCESS){
			return status;
		}

		return ErrorCode.SUCCESS;
	}

	@Override
	public int thirdQuestion(String exam) {
		//获取组卷策略
		StrategyTwo two = paperStrategyService.getStrategyTwo();
		String examCategory = exam.substring(0, 2);
		boolean flag = checkExamIsUse(PaperPattern.KE_GUAN,3,exam);
		if(flag){
			return 3011;
		}
		int count;
		int categoryCount = selectUsedByPaperId(PaperPattern.KE_GUAN, 3, examCategory);
		switch (examCategory) {
			//儿科整套手法
			case ExamCategory.ER_KE_ZT:
				//未按照组卷策略添加
				count = two.getExamEK();
				if (count == 0) {
					return 3009;
				}

				//如果数量相等 ,则该分类不能进行使用
				if (categoryCount >= two.getExamEK()) {
					return 3010;
				}
				break;
			//伤科整套手法
			case ExamCategory.SHANG_KE_ZT:
				count = two.getExamSK();
				if (count == 0) {
					return 3009;
				}
				//如果数量相等 ,则该分类不能进行使用
				if (categoryCount >= two.getExamSK()) {
					return 3010;
				}
				break;
			//妇科整套手法
			case ExamCategory.FU_KE_ZT:
				count = two.getExamFK();
				if (count == 0) {
					return 3009;
				}
				//如果数量相等 ,则该分类不能进行使用
				if (categoryCount >= two.getExamFK()) {
					return 3010;
				}
				break;
			//内科整套手法
			case ExamCategory.NEI_KE_ZT:
				count = two.getExamNK();
				if (count == 0) {
					return 3009;
				}
				//如果数量相等 ,则该分类不能进行使用
				if (categoryCount >= two.getExamNK()) {
					return 3010;
				}
				break;
			default:
				//未按照组卷策略添加
				return 3009;
		}
		return 200;
	}


}
